from contextlib import asynccontextmanager

from fastapi import FastAPI
from tortoise.contrib.fastapi import RegisterTortoise

from config import ServiceConfig, ORM_LINK_CONF
from apps.libs.init import init_app


@asynccontextmanager
async def lifespan(app: FastAPI):
    orm = RegisterTortoise(
        app,
        ORM_LINK_CONF,
    )
    await orm.init_orm()
    yield

def create_app() -> FastAPI:
    app = FastAPI(include_in_schema=ServiceConfig.INCLUDE_IN_SCHEMA, lifespan=lifespan)

    app = init_app(app)

    return app
